<template>
  <van-cell class="comment-item">
    <!-- 用户头像 -->
    <van-image
      slot="icon"
      class="avatar"
      round
      fit="cover"
      :src="comment.aut_photo"
    />
    <div slot="title" class="title-wrap">
      <div class="user-name">{{ comment.aut_name }}</div>
      <!-- 点赞按钮 -->
      <van-button
        class="like-btn"
        :icon="comment.is_liking ? 'good-job' : 'good-job-o'"
        :style="{ color: comment.is_liking ? 'red' : '' }"
        @click="onCommentLike"
        :loading="commentLoading"
        >{{ comment.like_count || "赞" }}</van-button
      >
    </div>
    <!-- 回复模块 -->
    <div slot="label">
      <p class="comment-content">{{ comment.content }}</p>
      <div class="bottom-info">
        <span class="comment-pubdate">{{
          comment.pubdate | relativeTime
        }}</span>
        <van-button
          class="reply-btn"
          round
          @click="$emit('reply-click', comment)"
          >回复 {{ comment.reply_count }}</van-button
        >
      </div>
    </div>
  </van-cell>
</template>

<script>
// 引入点赞和取消点赞的接口
import { addCommentLike, deleteCommentLike } from "@/api/comment";
export default {
  name: "CommentItem",
  props: {
    comment: {
      type: Object,
      required: true,
    },
  },
  created() {},
  data() {
    return {
      commentLoading: false, //加载评论的状态用于节流
    };
  },

  methods: {
    // 请求点赞和取消点赞
    async onCommentLike() {
      // 每次请求前开启加载状态
      this.commentLoading = true;
      try {
        //判断是否点赞了
        if (this.comment.is_liking) {
          // 已赞 取消点赞
          await deleteCommentLike(this.comment.com_id);
          //   点赞数--
          this.comment.like_count--;
        } else {
          // 没有点赞 就点赞
          await addCommentLike(this.comment.com_id);
          //点赞数++
          this.comment.like_count++;
        }
        // 点赞状态取反
        this.comment.is_liking = !this.comment.is_liking;
      } catch (err) {
        let message = "";
        if (err.response && err.response.status) {
          switch (err.response.status) {
            case 401:
              message = "用户未认证";
              break;
            case 404:
              message = "访问路径错误";
              break;
            case 507:
              message = "数据库异常";
              break;
          }
        }
        this.$toast(message);
      }
      // 每次请求完关闭加载状态
      this.commentLoading = false;
    },
  },
};
</script>

<style scoped lang="less">
.comment-item {
  .avatar {
    width: 72px;
    height: 72px;
    margin-right: 25px;
  }
  .title-wrap {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .user-name {
      color: #406599;
      font-size: 26px;
    }
  }
  .comment-content {
    font-size: 32px;
    color: #222222;
    word-break: break-all;
    text-align: justify;
  }
  .comment-pubdate {
    font-size: 19px;
    color: #222;
    margin-right: 25px;
  }
  .van-button--normal {
    padding: 0;
  }
  .bottom-info {
    display: flex;
    align-items: center;
  }
  .reply-btn {
    width: 135px;
    height: 48px;
    line-height: 48px;
    font-size: 21px;
    color: #222;
  }
  .like-btn {
    height: 30px;
    padding: 0;
    border: none;
    font-size: 19px;
    line-height: 30px;
    margin-right: 7px;
    .van-icon {
      font-size: 30px;
    }
  }
}
</style>